//
//  HomePageViewController.swift
//  zpzk_shop
//
//  Created by Qingke on 16/5/19.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit

class HomePageViewController: UIViewController {

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var pageController: UIPageControl!
    @IBOutlet weak var shopTableView: UITableView!
    
    var pageWidth: CGFloat = 0
    var pageHeight: CGFloat = 0

    let url: String = "http://api.zpzk100.com/client/brand_theme_index"

    var activities = [Activity]()
    var showActivities = [Activity]()
    var activitiesShop = [ActivityShop]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //注册使用activityshopCell
        shopTableView.registerClass(ActivityShopCell.self, forCellReuseIdentifier: "ActivityShopCell")
        shopTableView.registerNib(UINib(nibName: "ActivityShopCell",bundle: nil), forCellReuseIdentifier: "ActivityShopCell")
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        scrollView.delegate = self
        Activity.loadData(self)
        
    }
    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
    }
    
    func loadScrollView() {
        //清除scrollview的之前的子视图
        for subScrollVIew in scrollView.subviews {
            subScrollVIew.removeFromSuperview()
        }
        self.pageController.numberOfPages = activities.count //设置显示的图片张数与pageController个数对应
        initScrollView()
        
    }
    
    func initScrollView() {
        pageController.currentPage = 1
        
        pageWidth = self.view.frame.size.width
        pageHeight = self.scrollView.frame.height
        
        scrollView.contentSize = CGSizeMake(pageWidth * CGFloat(self.activities.count), pageHeight)
        scrollView.pagingEnabled = true //是否支持分页
        scrollView.showsHorizontalScrollIndicator = false //是否可以纵向拉伸
        scrollView.showsVerticalScrollIndicator = true//是否可以横向拉伸
        scrollView.scrollsToTop = true//点击状态栏回到顶部
        scrollView.bounces = true
        
        
        addSubScrollViewImage()
        for (index,activity) in self.showActivities.enumerate() {
            let subScrollView = UIImageView(frame: CGRectMake(pageWidth * CGFloat(index), 0, pageWidth, pageHeight))
            if let scrollViewImage = activity.image_path {
                subScrollView.othersetImage(scrollViewImage, placeHold: UIImage(named: "photo-1")!)
            }
            scrollView.addSubview(subScrollView)
        }
        scrollView.setContentOffset(CGPoint(x: pageWidth, y: 0), animated: false)
        
        //定时器判定
    }
    
    func addSubScrollViewImage() {
        let page = pageController.currentPage
        switch page {
        case 0:
            self.updataImage(self.activities.count - 1, second: 0, third: 1)
        case self.activities.count - 1 :
            self.updataImage(page - 1, second: page, third: 0)
        default:
            self.updataImage(page - 1, second: page, third: page + 1)
        }
    }
    func updataImage(first: Int, second: Int, third: Int) {
        self.showActivities.removeAll()
        showActivities.append(activities[first])
        showActivities.append(activities[second])
        showActivities.append(activities[third])
    }
    func reflashScrollView() {
        self.addSubScrollViewImage()
        
        var subScrollView = scrollView.subviews as! [UIImageView]
        for (index,activity) in showActivities.enumerate() {
            if let imageUrl = activity.image_path {
                subScrollView[index].othersetImage(imageUrl, placeHold: UIImage(named: "photo-1")!)
            }
        }
        scrollView.setContentOffset(CGPoint(x: pageWidth, y: 0), animated: false)
    }
    
}
extension HomePageViewController: UIScrollViewDelegate {
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let currentPageDiff = scrollView.contentOffset.x / scrollView.frame.width
        
        if currentPageDiff == 2 {
           // 向左滑： 设置当前page
            if pageController.currentPage == self.activities.count - 1 {
                pageController.currentPage = 0
            } else {
                pageController.currentPage += 1
            }
            
        } else if currentPageDiff == 0{
            //向右滑
            if pageController.currentPage == 0 {
                pageController.currentPage = self.activities.count - 1
            }else {
                pageController.currentPage -= 1
            }
        }
        reflashScrollView()
    }
    
}
extension HomePageViewController: UITableViewDelegate {
    
}
extension HomePageViewController: UITableViewDataSource {
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return activitiesShop.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("ActivityShopCell") as! ActivityShopCell
        
        let shop = activitiesShop[indexPath.row]
        cell.shopName.text = shop.shopName

        
        cell.shopDiscount.text = "\(shop.shopDiscount)" + "折"
        
        cell.shopImage.othersetImage(shop.shopImageUrl, placeHold: UIImage(named: "photo-1")!)

        return cell
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 140
    }
}

